import pandas as pd
import jieba
from jieba import analyse
import numpy as np

df = pd.read_csv('static/data/pre_job_info.csv')
df_bar = pd.read_csv('static/data/bar_word.csv')
df_word_cloud = pd.read_csv('static/data/wordCloud_word.csv')


def data_line():
    line_data = (df[['职位名称', '发布时间']]
                 .groupby('发布时间')
                 .count()
                 .reset_index().rename(columns={'发布时间': 'name', '职位名称': 'value'})
                 .sort_values("name", inplace=False, ascending=False).to_numpy())
    return {
        'name': line_data[:, 0].tolist(),
        'value': line_data[:, 1].tolist()
    }


def data_sunburst():
    df_sunburst_parent = df[['职位名称', '学历条件']].groupby(['学历条件']).count().reset_index().values.tolist()
    df_sunburst_children = df[['职位名称', '学历条件', '是否经验']].groupby(
        ['学历条件', '是否经验']).count().reset_index()
    pie_data = [
        {
            'name': i[0],
            'value': i[1],
            'children': [{
                'name': j[1],
                'value': int(j[2])
            }
                for j in
                df_sunburst_children[df_sunburst_children['学历条件'] == i[0]].values]
        }
        for i in df_sunburst_parent]
    return {
        'data': pie_data
    }


def data_sankey():
    sankey_data = [
        {'name': '热门'},
        {'name': '非热门'}
    ]
    df_sankey = df[['职位名称', '是否热门', '职位类型']].groupby(['是否热门', '职位类型']).count().reset_index()
    sankey_data += [{'name': i} for i in set(df_sankey['职位类型'].values.tolist())]
    df_sankey_hot = df_sankey[df_sankey['是否热门']].values
    df_sankey_no_hot = df_sankey[df_sankey['是否热门'] == False].values
    sankey_link = [{'source': '热门', 'target': i[1], 'value': int(i[2])} for i in df_sankey_hot] + [
        {'source': '非热门', 'target': i[1], 'value': int(i[2])} for i in df_sankey_no_hot]
    df_sankey_area = df.drop('工作地点', axis=1).join(
        df['工作地点'].str.split(',', expand=True).stack().reset_index(level=1, drop=True).rename('工作地点'))
    df_sankey_area = df_sankey_area[['职位名称', '工作地点', '职位类型']].groupby(
        ['职位类型', '工作地点']).count().reset_index()
    sankey_link += [{'source': i[0], 'target': i[1], 'value': int(i[2])} for i in df_sankey_area.values]
    sankey_data += [{'name': i} for i in set(df_sankey_area['工作地点'].values.tolist())]
    return {
        'data': sankey_data,
        'link': sankey_link
    }


def data_bar():
    df_job_count_top5 = df_bar.sort_values("count", inplace=False, ascending=False).head()
    return {
        'name': df_job_count_top5['word'].values.tolist(),
        'value': df_job_count_top5['count'].values.tolist()
    }


def data_word_cloud():
    df_keyword_top50 = df_word_cloud.sort_values("count", inplace=False, ascending=False).head(50)
    word_cloud_data = df_keyword_top50.values.tolist()
    word_cloud_data = [{'name': i[0], 'value': i[1]} for i in word_cloud_data]
    return {
        'data': word_cloud_data
    }
